package cg.cmm.base.log.listener;

import cg.cmm.base.exceptions.ApplicationException;
import cg.cmm.base.log.aspect.LogProductEvent;
import cg.cmm.base.log.dto.LogDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author: baibing
 * create time: 2024-11-17 20:44
 * description:
 */
@Slf4j
@Component
public class LogListener {
    @SneakyThrows
    @Async
    @EventListener(LogProductEvent.class)
    public void onApplicationEvent(LogProductEvent event) {
        LogDTO logDTO = event.getLogDTO();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(logDTO));
        } catch (JsonProcessingException e) {
            throw new ApplicationException(e);
        }

    }
}
